package com.imedway.ihe.dd.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.http.ContentType;
import com.imedway.ihe.dd.dto.ProvidedRegisterDTO;
import com.imedway.ihe.dd.helper.Iti41ProvidedRegisterHelper;
import com.imedway.ihe.dd.properties.ItiAddressProperties;
import com.imedway.ihe.dd.properties.ItiAddressProperties;
import com.imedway.ihe.dd.util.QueryRegistryUtil;
import com.imedway.ihe.dd.util.RegisterDocumentSetUtil;
import com.imedway.ihe.dd.vo.FindDocumentsVO;
import com.imedway.ihe.dd.vo.Iti18QueryVO;
import io.swagger.annotations.Api;
import org.apache.camel.*;
import org.apache.camel.support.DefaultExchange;
import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.QueryReturnType;
import org.openehealth.ipf.commons.ihe.xds.core.responses.QueryResponse;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Response;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryResponse;
import org.openehealth.ipf.platform.camel.core.util.Exchanges;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.XdsRenderingUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.activation.DataHandler;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.imedway.ihe.constant.Constants.ITI18_ENDPOINT_URL;
import static com.imedway.ihe.constant.Constants.ITI42_ENDPOINT_URL;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/4
 */
@Slf4j
@RestController
@RequestMapping("/ihe/xds")
@RequiredArgsConstructor
@Api(tags = "XDS-Iti42文档注册")
public class Iti42ClientController {

    private final CamelContext camelContext;
    private final ProducerTemplate producerTemplate;
    private final ItiAddressProperties itiAddressProperties;

    @PostMapping("iti42")
    public Response iti42(@RequestBody ProvidedRegisterDTO providedRegisterDTO) throws InvalidPayloadException {
        Exchange exchange = new DefaultExchange(camelContext);
        exchange.setPattern(ExchangePattern.InOut);

        // 构建数据
        RegisterDocumentSet registerDocumentSet = Iti41ProvidedRegisterHelper.createRegisterDocumentSet(providedRegisterDTO);
//        RegisterDocumentSet registerDocumentSet = RegisterDocumentSetUtil.createRegisterDocumentSet();

        SubmitObjectsRequest submitObjectsRequest = EbXML30Converters.convert(registerDocumentSet);

        // 发送请求
        exchange.getIn().setBody(submitObjectsRequest);

        log.debug("请求参数: \n" + XdsRenderingUtils.render(exchange));

        exchange = producerTemplate.send("xds-iti42://" + itiAddressProperties.getIti42Location(), exchange);

        Exception exception = Exchanges.extractException(exchange);
        if (exception != null) {
            log.error(ExceptionUtil.stacktraceToString(exception));
        }
        return exchange.getMessage().getMandatoryBody(Response.class);
    }

}
